package com.example.secondhandmall.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;

import com.example.secondhandmall.R;
import com.example.secondhandmall.adapter.OrderAdapter;
import com.example.secondhandmall.adapter.PutedAdapter;
import com.example.secondhandmall.api.Api;
import com.example.secondhandmall.api.ApiConfig;
import com.example.secondhandmall.api.MyCallback;
import com.example.secondhandmall.entity.GoodsEntity;
import com.example.secondhandmall.entity.GoodsListResponse;
import com.example.secondhandmall.entity.GoodsSimpleEntity;
import com.example.secondhandmall.entity.PhotoEntity;
import com.example.secondhandmall.util.StringUtils;
import com.google.gson.Gson;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class PutedActivity extends BaseActivity {

    private RecyclerView recyclerView;
    private PutedAdapter putedAdapter;
    private List<GoodsSimpleEntity> datas;
    private Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    putedAdapter.setDatas(datas);
                    putedAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    private ImageView ivReturn;

    @Override
    protected int initLayout() {
        return R.layout.activity_puted;
    }

    @Override
    protected void initView() {
        recyclerView = findViewById(R.id.recyclerView);
        ivReturn = findViewById(R.id.iv_return);
    }

    @Override
    protected void initData() {
        ivReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);

        putedAdapter = new PutedAdapter(this);
        putedAdapter.setOnItemClickListener(new PutedAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int id) {
                navigateTo(DetailActivity.class, "detail", id);
            }
        });
        recyclerView.setAdapter(putedAdapter);

        getOrderList();
//        localOrderList();
    }

    private void getOrderList() {
        String account = getStringFromSp("account");
        if (!StringUtils.isEmpty(account)) {
            HashMap<String, Object> params = new HashMap<>();
            params.put("userId", account);
            Api.config(ApiConfig.GOODS_LIST_BY_CATEGORY, params).getRequest(new MyCallback() {
                @Override
                public void onSuccess(String res) {
                    System.out.println(res);
                    GoodsListResponse putedListResponse = new Gson().fromJson(res, GoodsListResponse.class);
                    if (putedListResponse != null && putedListResponse.getCode() == 200) {
                        datas = putedListResponse.getData();
                        if (datas != null && datas.size() > 0) {
                            mHandler.sendEmptyMessage(0);
                        } else {
                            showToastSync("暂时还没有上架商品。");
                        }
                    }
                }

                @Override
                public void onFailure(Exception e) {
                    showToastSync("请求失败！");
                }
            });
        } else {
            navigateTo(LoginActivity.class);
            showToast("请先登录");
        }
    }

    private void localOrderList() {
        List<GoodsSimpleEntity> list = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            GoodsSimpleEntity goodsSimpleEntity = new GoodsSimpleEntity();
            goodsSimpleEntity.setGoodsName("垃圾桶家用带盖按压式");
            goodsSimpleEntity.setGoodsPrice(17.9);
            goodsSimpleEntity.setDel(1);
            List<PhotoEntity> photoEntities = new ArrayList<>();
            PhotoEntity photoEntity1 = new PhotoEntity();
            photoEntity1.setGoodsPhotoUrl("https://img.alicdn.com/bao/uploaded/i2/1062905262/O1CN012f48G61ok2lbZefFV_!!0-item_pic.jpg_200x200q90.jpg_.webp");
            PhotoEntity photoEntity2 = new PhotoEntity();
            photoEntity2.setGoodsPhotoUrl("https://img.alicdn.com/bao/uploaded/i2/2205308485750/O1CN01q9ySzz1sLY4UnDOWB_!!0-item_pic.jpg_200x200q90.jpg_.webp");
            photoEntities.add(photoEntity1);
            photoEntities.add(photoEntity2);
            goodsSimpleEntity.setUrl(photoEntity1);

            goodsSimpleEntity.setUserName("数家珍");
            goodsSimpleEntity.setUserPhoto("https://img.alicdn.com/bao/uploaded/i2/1062905262/O1CN012f48G61ok2lbZefFV_!!0-item_pic.jpg_200x200q90.jpg_.webp");
//            goodsSimpleEntity.setDetail("品牌名称：数家珍\n" +
//                    "产品参数：\n" +
//                    "\n" +
//                    "品牌: 数家珍颜色分类: 压圈-白色小号【买2赠60只垃圾袋】 压圈-蓝色小号【买2赠60只垃圾袋】 压圈-白色大号【买2赠60只垃圾袋】 压圈-蓝色大号【买2赠60只垃圾袋】 密封弹盖-白色小号【买2赠60只垃圾袋】 密封弹盖-蓝色小号【买2赠60只垃圾袋】 密封弹盖-白色大号【买2赠60只垃圾袋】 按压式弹盖-蓝色大号【买2赠60只垃圾袋】 【压圈提手款】蓝色小号 【压圈提手款】粉色小号 【压圈提手款】蓝色大号 【压圈提手款】粉色大号 【弹盖提手款】蓝色小号 【弹盖提手款】粉色小号 【弹盖提手款】蓝色大号 【弹盖提手款】粉色大号垃圾桶类型: 收纳桶货号: E5738~外壳材质: 塑料适用场景: 家庭使用");
//            goodsSimpleEntity.setCollectFlag(false);
            goodsSimpleEntity.setGoodsId(22);

            list.add(goodsSimpleEntity);
        }
        for (int i = 0; i < 4; i++) {
            GoodsSimpleEntity goodsSimpleEntity = new GoodsSimpleEntity();
            goodsSimpleEntity.setGoodsName("垃圾桶家用带盖按压式");
            goodsSimpleEntity.setGoodsPrice(17.9);
            goodsSimpleEntity.setDel(0);
            List<PhotoEntity> photoEntities = new ArrayList<>();
            PhotoEntity photoEntity1 = new PhotoEntity();
            photoEntity1.setGoodsPhotoUrl("https://img.alicdn.com/bao/uploaded/i2/1062905262/O1CN012f48G61ok2lbZefFV_!!0-item_pic.jpg_200x200q90.jpg_.webp");
            PhotoEntity photoEntity2 = new PhotoEntity();
            photoEntity2.setGoodsPhotoUrl("https://img.alicdn.com/bao/uploaded/i2/2205308485750/O1CN01q9ySzz1sLY4UnDOWB_!!0-item_pic.jpg_200x200q90.jpg_.webp");
            photoEntities.add(photoEntity1);
            photoEntities.add(photoEntity2);
            goodsSimpleEntity.setUrl(photoEntity1);

            goodsSimpleEntity.setUserName("数家珍");
            goodsSimpleEntity.setUserPhoto("https://img.alicdn.com/bao/uploaded/i2/1062905262/O1CN012f48G61ok2lbZefFV_!!0-item_pic.jpg_200x200q90.jpg_.webp");
//            goodsSimpleEntity.setDetail("品牌名称：数家珍\n" +
//                    "产品参数：\n" +
//                    "\n" +
//                    "品牌: 数家珍颜色分类: 压圈-白色小号【买2赠60只垃圾袋】 压圈-蓝色小号【买2赠60只垃圾袋】 压圈-白色大号【买2赠60只垃圾袋】 压圈-蓝色大号【买2赠60只垃圾袋】 密封弹盖-白色小号【买2赠60只垃圾袋】 密封弹盖-蓝色小号【买2赠60只垃圾袋】 密封弹盖-白色大号【买2赠60只垃圾袋】 按压式弹盖-蓝色大号【买2赠60只垃圾袋】 【压圈提手款】蓝色小号 【压圈提手款】粉色小号 【压圈提手款】蓝色大号 【压圈提手款】粉色大号 【弹盖提手款】蓝色小号 【弹盖提手款】粉色小号 【弹盖提手款】蓝色大号 【弹盖提手款】粉色大号垃圾桶类型: 收纳桶货号: E5738~外壳材质: 塑料适用场景: 家庭使用");
//            goodsSimpleEntity.setCollectFlag(false);
            goodsSimpleEntity.setGoodsId(22);

            list.add(goodsSimpleEntity);
        }
        putedAdapter.setDatas(list);
        putedAdapter.notifyDataSetChanged();
    }
}